package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.http.entity.MyHappyCoinDataItem;
import com.kkd.kkdapp.http.entity.MyHappyCoinItemBean;
import com.kkd.kkdapp.pinnedsection.PinnedSectionRefreshListView.PinnedSectionListAdapter;

/**
 * 分好类的适配器.加载的是分好组的list数据
 */
public class MyHappyCoinAdapter extends BaseAdapter implements
		PinnedSectionListAdapter {
	// 时间以及相关信息
	private ArrayList<MyHappyCoinItemBean> list;
	private Context mContext;
	private LayoutInflater inflater;
	private DecimalFormat df = new DecimalFormat("###,##0.00");

	public ArrayList<MyHappyCoinItemBean> getList() {
		return list;
	}

	public void setList(ArrayList<MyHappyCoinItemBean> list) {
		if (list != null) {
			this.list = list;
		} else {
			list = new ArrayList<MyHappyCoinItemBean>();
		}
	}

	public MyHappyCoinAdapter(ArrayList<MyHappyCoinItemBean> list,
			Context mContext) {
		super();
		this.setList(list);
		this.mContext = mContext;
		inflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public MyHappyCoinItemBean getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MyHappyCoinItemBean bean = getItem(position);
		ViewHolderPinner viewHolderPinner = null;
		ViewHolderContent viewHolderContent = null;
		if (convertView == null) {
			if (bean.type == MyHappyCoinItemBean.SECTION) {// 悬停的标题
				viewHolderPinner = new ViewHolderPinner();
				convertView = inflater.inflate(
						R.layout.pinner_myhappycoin_listview, parent, false);
				viewHolderPinner.tv_title_pinner = (TextView) convertView
						.findViewById(R.id.tv_title_pinner);
				convertView.setTag(viewHolderPinner);
			} else {
				viewHolderContent = new ViewHolderContent();
				convertView = inflater.inflate(
						R.layout.item_content_pinner_myhappycoin_listview,
						parent, false);
				viewHolderContent.tv_title = (TextView) convertView
						.findViewById(R.id.tv_title);
				viewHolderContent.tv_time = (TextView) convertView
						.findViewById(R.id.tv_time);
				viewHolderContent.tv_num = (TextView) convertView
						.findViewById(R.id.tv_num);
				viewHolderContent.tv_flag = (TextView) convertView
						.findViewById(R.id.tv_flag);
				viewHolderContent.line = convertView.findViewById(R.id.line);
				viewHolderContent.bottom = convertView
						.findViewById(R.id.bottom);
				convertView.setTag(viewHolderContent);
			}
		} else {
			if (bean.type == MyHappyCoinItemBean.SECTION) {// 悬停的标题
				viewHolderPinner = (ViewHolderPinner) convertView.getTag();
			} else {
				viewHolderContent = (ViewHolderContent) convertView.getTag();
			}
		}
		MyHappyCoinDataItem item = list.get(position).getDealDetailItem();
		String createTime = item.getCreateTime();
		// 当item属于标题的时候,就只显示分类的日期yyyy-MM-dd
		if (bean.type == MyHappyCoinItemBean.SECTION) {
			viewHolderPinner.tv_title_pinner.setText(createTime.substring(0, 4)
					+ "年" + createTime.substring(5) + "月");
		} else {// 当item属于内容的时候,就只显示分类的日期HH:mm:ss,和其他类容

			viewHolderContent.tv_title.setSelected(true);
			viewHolderContent.tv_title.setText(item.getDesc());
			viewHolderContent.tv_time.setText(item.getCreateTime());
			if (item.getCoinCount().contains("+")) {
				viewHolderContent.tv_num.setTextColor(Color
						.parseColor("#FF7301"));
			} else {
				viewHolderContent.tv_num.setTextColor(Color
						.parseColor("#38BCA4"));
			}
			viewHolderContent.tv_num.setText(item.getCoinCount());
			if (position == list.size() - 1
					|| getItem(position + 1).type == MyHappyCoinItemBean.SECTION) {
				viewHolderContent.line.setVisibility(View.GONE);
			} else {
				viewHolderContent.line.setVisibility(View.VISIBLE);
			}
		}
		return convertView;

	}

	// 判断是否是属于标题悬浮的
	@Override
	public boolean isItemViewTypePinned(int viewType) {
		return viewType == MyHappyCoinItemBean.SECTION;
	}

	// arraylist的数据里面有2种类型,标题和内容
	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		return ((MyHappyCoinItemBean) getItem(position)).type;
	}

	class ViewHolderPinner {
		TextView tv_title_pinner;
	}

	class ViewHolderContent {
		TextView tv_title;
		TextView tv_time;
		TextView tv_num;
		TextView tv_flag;
		View line;
		View bottom;
	}
}
